package com.project.controller;

import com.project.bean.ColumnBean;
import com.project.service.IColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("echarts")
public class ColumnController {
    @Autowired
    private IColumnService service;

    @RequestMapping("selectYear")
    public List<Integer> findYears() {
        return service.findYears();
    }

    @RequestMapping("selectMonthValue")
    public List findAll(String year) {
        List<ColumnBean> allList = service.findAll(year);
        //类别集合
        List listA = new ArrayList();
        listA.add(0, "类别");
        List listC = allList.stream().map(n -> n.getType()).distinct().toList();
        for (int i = 0; i < listC.size(); i++) {
            listA.add(listC.get(i));
        }
//        需要传到前端的结合
        List<Object[]> monthList = new ArrayList<>();
        monthList.add(listA.toArray());
        for (int i = 0; i < 12; i++) {
            monthList.add(null);
        }
        for (int i = 1; i < listA.size(); i++) {
            for (int j = 0; j < allList.size(); j++) {
                ColumnBean column = allList.get(j);
                if (column.getType().equals(listA.get(i).toString())) {
                    for (int k = 1; k <= 12; k++) {
                        if (column.getMonth().equals(k + "月")) {
                            if (monthList.get(k) == null) {
                                Object[] newList = new Object[listA.size()];
                                newList[0] = k + "月";
                                newList[i] = column.getNum();
                                monthList.set(k, newList);
                            } else {
                                monthList.get(k)[i] = column.getNum();
                            }
                            allList.remove(j);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < monthList.size(); i++) {
            if (monthList.get(i) == null) {
                monthList.remove(i);
                i--;
                continue;
            }
            else {
                for (int j = 0; j < monthList.get(i).length; j++) {
                    if (monthList.get(i)[j]==null){
                        monthList.get(i)[j]=0;
                    }
                }
            }
        }
        return monthList;
    }
}
